package com.woyou.ui.adapter;

import java.util.List;

import com.citaq.ideliver.R;
import com.woyou.bean.OrderGoodsItem;
import com.woyou.bean.OrderOptionItem;
import com.woyou.bean.OrderSubGoodsItem;
import com.woyou.utils.ParseUtils;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 订单明细
 * 
 * @author shenzhou 2015年1月17日
 */
public class OrderDetailAdapter extends BaseAdapter {

	private List<OrderGoodsItem> mList;
	private Context mContext;

	public OrderDetailAdapter(List<OrderGoodsItem> mList, Context context) {
		this.mList = mList;
		mContext = context;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.item_orderdetail_lv, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.num = (TextView) convertView.findViewById(R.id.num);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.option = (TextView) convertView.findViewById(R.id.option);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		OrderGoodsItem orderGoodsItem = mList.get(position);
		holder.name.setText(orderGoodsItem.getgName());
		holder.num.setText("x" + orderGoodsItem.getQty() + "");
		
		String money = ParseUtils.String2Money(orderGoodsItem.getPrice());
		int indexOf = money.indexOf("-");
		if ( indexOf==-1 ){
			holder.price.setTextColor(Color.BLACK);
		} else {
			holder.price.setTextColor(Color.RED);
		}
		holder.price.setText(Html.fromHtml("￥<big>" + ParseUtils.String2Money(orderGoodsItem.getPrice()) + "</big>"));
		//各种套餐判断
		List<OrderOptionItem> optList = orderGoodsItem.getOptList();
		List<OrderSubGoodsItem> subGList = orderGoodsItem.getSubGList();
		if ( (null != optList && optList.size()>0) || (null != subGList && subGList.size()>0)) {   //商品属性
			holder.option.setText("(" + getOpName(optList, subGList) + ")");
		} else {
			holder.option.setVisibility(View.GONE);
		}
		
		return convertView;
	}
	
	
	public String getOpName(List<OrderOptionItem> optList, List<OrderSubGoodsItem> subGList){
		StringBuilder sb = new StringBuilder();
		String gName = null;
		String tempName = null;
		
		if ( optList != null && optList.size()>0 ) {
			for( OrderOptionItem opi : optList ){
				sb.append(opi.getOptName() + "+");
			}
			tempName = sb.toString();
			if( !TextUtils.isEmpty(tempName) && tempName.length()>1 ){
				gName = tempName.substring(0, tempName.length()-1);
			}
		}
		
		if ( subGList  != null && subGList.size()>0 ){
			for( OrderSubGoodsItem sg : subGList ){
				int num = sg.getQty();
				if( num>0 ){
					sb.append(sg.getgName() + "+");
				}else{
					sb.append(sg.getgName() + "X"+num+"+");
				}
			}
		}
		
		tempName = sb.toString();
		if( !TextUtils.isEmpty(tempName) && tempName.length()>1 ){
			gName = tempName.substring(0, tempName.length()-1);
		}
		return gName;
	}

	static class ViewHolder {
		TextView name;
		TextView num;
		TextView price;
		TextView option;
	}

}
